package gui;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import linguo.GameState;
import linguo.sprites.BufferedImageLoader;

@SuppressWarnings("serial")
public class VentanaTeclado extends JFrame {

	protected JButton btnAvanzar, btnGirarD, btnGirarI, btnBrazoT, btnBrazoD, btnPincel, btnDisparo, btnBlanco, btnNegro;
	protected JPanel contentPanel;
	protected GameGUI gui;
	protected VentanaPrincipal ventana;

	public VentanaTeclado(GameGUI g, VentanaPrincipal v) {
		gui = g;
		ventana = v;
		contentPanel = new JPanel();
		btnAvanzar = new JButton();
		btnGirarD = new JButton();
		btnGirarI = new JButton();
		btnBrazoT = new JButton();
		btnBrazoD = new JButton();
		btnPincel = new JButton();
		btnDisparo = new JButton();
		btnBlanco = new JButton();
		btnNegro = new JButton();

		contentPanel.add(btnAvanzar);
		contentPanel.add(btnGirarD);
		contentPanel.add(btnGirarI);
		contentPanel.add(btnBlanco);
		contentPanel.add(btnBrazoT);
		contentPanel.add(btnBrazoD);
		contentPanel.add(btnDisparo);
		contentPanel.add(btnNegro);
		contentPanel.add(btnPincel);

		this.setContentPane(contentPanel);
		configure();
		addListeners();
	}

	/** Configura el panel y los objetos del teclado */
	private void configure() {

		this.setLayout(null);
		this.setBounds(1800, 1800, 300, 200);
		this.setResizable(false);
		this.setTitle("Teclado");
		this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setLocation(1000, 300);

		contentPanel.setLayout(null);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

		btnAvanzar.setBounds(130, 10, 40, 40);
		btnGirarD.setBounds(190, 50, 40, 40);
		btnGirarI.setBounds(70, 50, 40, 40);
		btnPincel.setBounds(20, 120, 40, 40);
		btnDisparo.setBounds(130, 80, 40, 40);
		btnBrazoT.setBounds(210, 120, 60, 20);
		btnBrazoD.setBounds(210, 140, 60, 20);
		btnBlanco.setBounds(60, 120, 20, 20);
		btnNegro.setBounds(60, 140, 20, 20);

		BufferedImageLoader loader = new BufferedImageLoader();
		btnGirarD.setIcon(new ImageIcon(loader.loadImage(("/resources/Teclado/derecha.png"))));
		btnGirarI.setIcon(new ImageIcon(loader.loadImage(("/resources/Teclado/izquierda.png"))));
		btnAvanzar.setIcon(new ImageIcon(loader.loadImage(("/resources/Teclado/arriba.png"))));
		btnDisparo.setIcon(new ImageIcon(loader.loadImage(("/resources/Teclado/disparo.png"))));
		
		btnPincel.setIcon(new ImageIcon(loader.loadImage(("/resources/Teclado/pincel.png"))));
		btnBlanco.setIcon(new ImageIcon(loader.loadImage(("/resources/Teclado/blanco.png"))));
		btnNegro.setIcon(new ImageIcon(loader.loadImage(("/resources/Teclado/negro.png"))));
		
		btnBrazoT.setIcon(new ImageIcon(loader.loadImage(("/resources/Teclado/grab.png"))));
		btnBrazoD.setIcon(new ImageIcon(loader.loadImage(("/resources/Teclado/drop.png"))));

	}

	/** Agrega listeners asociando cada boton con una accion del robot */
	private void addListeners() {

		btnAvanzar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (gui.getState() == GameState.INGAME) gui.getRobot().avanzar();
			}
		});

		btnGirarD.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (gui.getState() == GameState.INGAME) gui.getRobot().girarDerecha();
			}
		});

		btnGirarI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (gui.getState() == GameState.INGAME) gui.getRobot().girarIzquierda();
			}
		});

		btnPincel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (gui.getState() == GameState.INGAME) gui.getRobot().togglePincel();
			}
		});

		btnDisparo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (gui.getState() == GameState.INGAME) gui.getRobot().tryToFire();
			}
		});

		btnBrazoT.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (gui.getState() == GameState.INGAME) gui.getRobot().usarBrazo();
			}
		});

		btnBrazoD.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (gui.getState() == GameState.INGAME) gui.getRobot().dropColeccionable();
			}
		});

		btnBlanco.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (gui.getState() == GameState.INGAME) gui.getRobot().toggleColorPincel(0);
			}
		});

		btnNegro.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (gui.getState() == GameState.INGAME) gui.getRobot().toggleColorPincel(1);
			}
		});

	}

}
